/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* $file:        AiThinker_RD03.h
* $Date:        31-March-2025
* $Revision:    V1.2
* $Project:     XiaoFei BSP Library
* $Taobao:      https://item.taobao.com/item.htm?abbucket=17&id=719248225132
*
* -------------------------------------------------------------------- */

#ifndef __AiThinker_RD03_H
#define __AiThinker_RD03_H

#define RD03_V2
#define RD03_RCC_Usart(x)           RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, (FunctionalState)x)
#define RD03_RCC_TX(x)              RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, (FunctionalState)x)
#define RD03_RCC_RX(x)              RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, (FunctionalState)x)
#define RD03_GPIO_TX                GPIOA
#define RD03_Pin_TX                 GPIO_Pin_9
#define RD03_GPIO_RX                GPIOA
#define RD03_Pin_RX                 GPIO_Pin_10
#define RD03_Usart                  USART1
#define RD03_BaudRate               115200
#define RD03_Group                  NVIC_PriorityGroup_2
#define RD03_ITPre                  1
#define RD03_ITSub                  1
#define RD03_IRQn                   USART1_IRQn
#define RD03_IRQHandler             USART1_IRQHandler

#ifndef RD03_BufDef_def
#define RD03_BufDef_def
typedef struct
{
    #if defined(RD03_V1)
    unsigned char buf[90];      /*!< The receved RD03 Data buffer */
    #elif defined(RD03_V2)
    unsigned char buf[300];     /*!< The receved RD03 Data buffer */
    #endif
    unsigned int len_max;       /*!< The max length of RD03 data */
    unsigned int len;           /*!< The current length of RD03 data */
}RD03_BufDef;
#endif

#ifndef RD03_MoodDef_def
#define RD03_MoodDef_def
typedef enum
{
    Report_Mood = 0x04 ,        /*!< The RD03 report mood */
    Running_Mood = 0x64         /*!< The RD03 running mood */
}RD03_MoodDef;
#endif

#ifndef RD03_RunDataDef_def
#define RD03_RunDataDef_def
typedef struct
{
    unsigned char state;        /*!< The data is 0x01 if the decoding is correct, otherwise it is 0x00 */
    unsigned int range;         /*!< The RD03 range data */
}RD03_RunDataDef;
#endif

#ifndef RD03_RepDataDef_def
#define RD03_RepDataDef_def
typedef struct
{
    unsigned char state;        /*!< The data is 0x01 if the decoding is correct, otherwise it is 0x00 */
    unsigned int range;         /*!< The RD03 range data */
    #if defined(RD03_V1)
    unsigned int energy[16];    /*!< The RD03 energy data */
    #elif defined(RD03_V2)
    unsigned int energy[32];    /*!< The RD03 energy data */
    #endif
}RD03_RepDataDef;
#endif

/* 
* @function:    RD03_BufClear
* @parameter:   [in]    RD03            The handle of RD03.
* @parameter:   [in]    ClThis          Input 0x01 to clear all buffers, otherwise input 0x00.
* @return:      None.
*/
void RD03_BufClear(RD03_BufDef* RD03, unsigned char ClThis);

/* 
* @function:    RD03_BufGet
* @parameter:   [in]    RD03            The handle of RD03.
* @parameter:   [in]    us              The interval for obtaining two adjacent pointers.
* @return:      Return 0x01 if the data has get successful, otherwise return 0x00.
*/
unsigned char RD03_BufGet(RD03_BufDef* RD03, unsigned int us);

/* 
* @function:    RD03_Init
* @parameter:   [in]    RD03            The handle of RD03.
* @return:      None.
*/
void RD03_Init(RD03_BufDef* RD03);

/* 
* @function:    RD03_SetMood
* @parameter:   [in]    RD03            The handle of RD03.
* @parameter:   [in]    Mood            The mode will be set to RD03.
* @return:      None.
*/
void RD03_SetMood(RD03_BufDef* RD03, RD03_MoodDef Mood);

/* 
* @function:    RD03_RunningData
* @parameter:   [in]    RD03            The handle of RD03.
* @parameter:   [in]    Data            The data decoding result of RD03 in running mode.
* @return:      None.
*/
void RD03_RunningData(RD03_BufDef* RD03, RD03_RunDataDef* Data);

/* 
* @function:    RD03_ReportData
* @parameter:   [in]    RD03            The handle of RD03.
* @parameter:   [in]    Data            The data decoding result of RD03 in report mode.
* @return:      None.
*/
void RD03_ReportData(RD03_BufDef* RD03, RD03_RepDataDef* Data);

/* 
* @function:    RD03_IRQHandler
* @callback:    The function will be callback when get the data from RD03.
*/
void RD03_IRQHandler(void);

#endif


